assert = require('assert').ok

Q = require("q")

exports.pagination_find = pagination_find = (model, criteria, sort, pageNum,pageSize,cb) ->
  assert(cb)
  model.find(criteria).count (err,count) ->
    if err
      return cb err
    if count <= 0
      return cb null, {pageNum:pageNum,pageSize:pageSize,pageCount:0,count:0,items:[]}
    query = model.find(criteria).sort(sort).skip((pageNum-1)*pageSize).limit(pageSize)
    query.exec (err, feedbacks) ->
      if err
        return cb(err)
      cb null, {
        pageNum:pageNum, pageSize:pageSize,
        pageCount:Math.floor((count+pageSize-1)/pageSize),
        count:count, items: feedbacks }


exports.pagination_qfind = Q.denodeify pagination_find
 
 
